package com.authority.framework.web.controller;

import com.authority.framework.web.domain.SysMenuEntity;
import com.authority.framework.web.domain.SysUserEntity;
import com.authority.framework.web.service.SysMenuService;
import com.authority.framework.base.util.BeanUtils;
import com.authority.framework.base.util.StringUtils;
import com.authority.framework.web.deal.MenuTreeDeal;
import com.example.demo.RegisterKey;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program: authority
 * @description: 首页 控制层
 * @author: Mr.Yan
 * @create: 2018-11-16 10:02
 **/
@Controller
public class IndexController {
	// 登录地址
	@Value("${shiro.user.loginUrl}")
	private String loginUrl;

	@Autowired
	private SysMenuService sysMenuService;
	/*参数*/
	Map<String, String> paramMap = null;

	@RequestMapping(value = "/index")
	@ResponseBody
	public ModelAndView index(ModelAndView mv) {
		SysUserEntity user = null;
		Object obj = SecurityUtils.getSubject().getPrincipal();
		if (StringUtils.isNotNull(obj)){
			user = new SysUserEntity();
			BeanUtils.copyBeanProp(user, obj);
		}
		List<SysMenuEntity> menus = sysMenuService.findListByUserId(user.getUserId().intValue());
		/*循环遍历 子菜单*/
		menus = MenuTreeDeal.getChildPerms(menus, 0);

		mv.addObject("user",user);
		mv.addObject("menus",menus);
		System.out.println(user);
		System.out.println(menus);
		mv.setViewName("index");
		return mv;
	}

	/**
	 * @描述 未注册软件
	 * @参数 a
	 * @返回值 a
	 * @创建人 Mr.Yan
	 * @时间 2018/9/4
	 */
	@RequestMapping("/unRegister")
	public ModelAndView unRegister(HttpServletRequest request, HttpServletResponse response,ModelAndView mv) throws
			IOException {
		RegisterKey registerKey = new RegisterKey();
		boolean register = registerKey.isRegister();
		if (register){
			// 跳转登录
			response.sendRedirect(loginUrl);
		}
		mv.setViewName("error/unRegister");
		return mv;
	}
}
